﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Client.ViewModel;

namespace Client.View
{
    /// <summary>
    /// Interaction logic for QuizView.xaml
    /// </summary>
    public partial class QuizView : UserControl
    {
        public delegate void FinishClickEvent(int totalscore, int score, int quizId);
        public event FinishClickEvent OnFinishClicked;

        private QuizViewModel viewModel;
        /// <summary>
        /// A usercontrolhoz tartozo ViewModel, a property beallitja a datacontextet is
        /// </summary>
        public QuizViewModel ViewModel
        {
            get
            {
                return viewModel;
            }
            set
            {
                viewModel = value;
                this.DataContext = viewModel;
                // így már létezni fog a visualtreeben a scrollviewer
                lwQuestions.Loaded += lwQuestions_Loaded;
            }
        }

        /// <summary>
        /// A ViewModel lapozza a kérdéseket, ezért kap egy referenciát a ScrollViewerre
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void lwQuestions_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.ScrollViewer = GetScrollViewer(lwQuestions) as ScrollViewer;
            lwQuestions.Loaded -= lwQuestions_Loaded;
        }
        
        public QuizView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Kihámozza a ScrollViewer a controlból,
        /// Robert Mooretól, stackoverflown találtam a kódot
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static DependencyObject GetScrollViewer(DependencyObject o)
        {
            // Return the DependencyObject if it is a ScrollViewer
            if (o is ScrollViewer)
            { return o; }

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
            {
                var child = VisualTreeHelper.GetChild(o, i);

                var result = GetScrollViewer(child);
                if (result == null)
                {
                    continue;
                }
                else
                {
                    return result;
                }
            }
            return null;
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if(OnFinishClicked != null)
            {
                OnFinishClicked(viewModel.TotalScore,viewModel.Score,viewModel.Quiz.ID);
            }
        }
    }
}
